// pages/phone/phone.js
var app = getApp();
var CONFIG = require("../../config/config.js");
var share = require("../../utils/share.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        dataobj:'',
        time: '发送验证码',
        phone: '',
        oldphone:'',   //老手机号
        message: '',
        currentTime: 60,
        disabled: false,
        submitfalse: true,
        name:'',
        user_id:'',
        share:0,
        showfalse1:false,
        codefalse:true,
        is_user:0,
        type:2,  //2列表进入  1添加学员进入
        user_idnew:'',
        is_zycon:2,
        newconid:'',
        is_oneself:0, //0-不在自己名下 1-自己名下
        is_check: 0, //是否可以被选走 0:不可以 1:可以
    },


    backindex:function(){
        wx.navigateTo({
            url: '/pages/login',
        })
    },
    

    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function (options) {
        var that = this;
        var name = that.data.name;
        var phone = that.data.phone;
        var oldphone = that.data.oldphone;
        // share.sharelog();
        return {
            title: '认证',
            path: '/pages/public/identphone?user_name=' + name + '&user_id=0&phone=' + phone + '&share=1&oldphone=' + oldphone + '&is_certif=' + that.data.is_user + '&newconid=' + that.data.newconid + '&is_zycon=' + that.data.is_zycon+'',
        }
            // imageUrl: '/images/share_6.jpg',
    },

    // 点击自行认证
    userlink(){
        var that = this;
        wx.navigateTo({
            url: '../public/userident?phone='+that.data.phone+'',
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        that.setData({
            name: options.user_name,
            phone: options.phone,
            user_id: options.user_id,
            is_user: Number(options.is_certif),
        })
        // newconid
        if (options.newconid) {
            that.setData({
                newconid: options.newconid
            })
        }
        if (options.is_zycon){
            that.setData({
                is_zycon: options.is_zycon
            })
        }
        if (options.type) {
            that.setData({
                type: options.type
            })
        }
        if (options.share){
            that.setData({
                share: options.share,
                oldphone: options.oldphone,
            })
        }else{
            that.setData({
                oldphone: options.phone,
            })
            that.telblurtel(options.phone)
        }
    },

    // 手机号改变时
    phoneinput: function (e) {
        // console.log(e);
        this.setData({
            phone: e.detail.value
        })
        this.telblurtel(e.detail.value)
    },
    
    // 姓名改变时
    nameinput:function(e){
        this.setData({
            name: e.detail.value
        })
    },

    messageinput: function (e) {
        // console.log(e);
        this.setData({
            message: e.detail.value
        })
    },
    telblurtel(e){
        let tel=e
        let that=this
        if ((/^1\d{10}$/.test(tel))) {
            var obj = {
                phone: tel,
                consultant_id:  app.globalData.admin_id,   //课程顾问id
            }
            share.wxRequest('crmapi/user/entryStuInfoNew', obj, function (res) {
                if (Number(res.data.errcode) == 0) {
                    var data = res.data.data.res;
                    that.setData({
                        is_check: Number(data.is_check), //是否可以被选走 0:不可以 1:可以
                        is_oneself:Number(data.is_oneself),

                    })

                }else{
                    // wx.showToast({
                    //     title: res.data.msg,
                    //     icon: 'none',
                    //     duration: 1500
                    // })
                    that.setData({
                        is_check: 0, //是否可以被选走 0:不可以 1:可以
                        is_oneself: 0, //是否有课程顾问 0:没有课程顾问 1:有课程顾问
                    })
                }
            })
        }else{
            that.setData({
                is_check: 0, //是否可以被选走 0:不可以 1:可以
                is_oneself: 0, //是否有课程顾问 0:没有课程顾问 1:有课程顾问
            })
        }
    },

    // 点击发送验证码
    messageClick: function () {
        var that = this;
        var currentTime = that.data.currentTime;
        var phone = that.data.phone;
        // console.log(phone);
        if (phone != '') {
            if ((/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone))) {
                var time = setInterval(function () {
                    that.setData({
                        time: currentTime + 's',
                        disabled: true
                    })
                    currentTime--;
                    if (currentTime <= 0) {
                        clearInterval(time);
                        that.setData({
                            time: '发送验证码',
                            currentTime: 60,
                            disabled: false
                        })
                    }
                }, 1000)
                share.wxRequest('crmapi/User/smsCode', { tel: phone,from:2}, function (res) {
                        // console.log(res);
                    if (Number(res.data.errcode) == 0) {
                        wx.showToast({
                            title: '发送成功',
                            icon: 'none',
                            duration: 1500
                        })
                    } else {
                        wx.showToast({
                            title: res.data.msg,
                            icon: 'none',
                            duration: 1500
                        })
                    }
                })

            } else {
                wx.showToast({
                    title: '请输入正确手机号',
                    icon: 'none',
                    duration: 1500
                })
            }
        } else {
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                duration: 1500
            })
        }
    },
    
    sureclick1(){
        var that = this;
        that.setData({
            showfalse1: false,
            codefalse:false,
        })
    },

    sureclick(){
        var that = this;
        that.setData({
            showfalse1: false,
        })
        console.log(that.data.type);
        if(Number(that.data.type)==1){
            var dataobj = that.data.dataobj;
            wx.redirectTo({
                url: '/pages/staff/choose_course?name=' + dataobj.name + '&tel=' + dataobj.phone + '&user_id=' + that.data.user_idnew + '&is_zycon=' + that.data.is_zycon + '&newconid=' + that.data.newconid+'&is_check='+that.data.is_check+'&is_oneself='+that.data.is_oneself,
            })
        }else{
            wx.navigateBack();
        }
    },

    // 点击提交
    submitbut: function (e) {
        
        var e1 = e.detail.value;
        var that = this;
        var nae = e1.truename;
        var name = nae.replace(/\s+/g, "");
        if (name == '') {
            wx.showToast({
                title: '请填写姓名',
                icon: 'none',
                duration: 1500
            })
        } else if (!(/^[\u2E80-\u9FFF]+$/.test(name))) {
            wx.showToast({
                title: '姓名只能为汉字',
                icon: 'none',
                duration: 1500
            })
        } else if (name.length > 5) {
            wx.showToast({
                title: '姓名最多为5个字',
                icon: 'none',
                duration: 1500
            })
        }else if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(e1.phone)) || e1.phone == '') {
            wx.showToast({
                title: '请填写正确的手机号',
                icon: 'none',
                duration: 1500
            })
        } else if (e1.message == '') {
            wx.showToast({
                title: '请先获取验证码',
                icon: 'none',
                duration: 1500
            })
        } else {
            wx.showLoading({
                title: '认证中~',
            })
            that.setData({
                submitfalse: false
            })
            var data = {
                phone: e1.phone,  //新手机号
                oldpPone: that.data.oldphone,
                code: e1.message,
                name: name,
                consultant_id: app.globalData.admin_id
            }
            that.setData({
                dataobj: data
            })
            
           
            share.wxRequest('crmapi/User/isCertif', data, function (res) {
                wx.hideLoading();
                // console.log(res);
                if (Number(res.data.errcode) == 0) {
                    that.setData({
                        showfalse1:true,
                        user_idnew: res.data.data.user_id
                    })
                } else {
                    that.setData({
                        submitfalse: true
                    })
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 1500
                    })
                }
            })
        }
    },



})